<?php
require_once 'Loans.php';
require_once 'Payments.php';
require_once 'LoanStatusChanges.php';
require_once 'LoanWriteOffCharges.php';
require_once 'LoanWriteOffPayments.php';
require_once '../application/modules/savings/models/SavingsAccounts.php';

class Loan_LoanController extends HTech_Controller_Action
{
    function init()
    {
        $cfg = array(
            'model' => 'Loans', 
            'form' => 'Emms_Form_Loan_Loan',
            'config' => array('add' => 'loans'),
            'redirect' => 'add',
            'flashMessenger' => true,
            'actionContext' => array(
                'plan' => array('prn','mob'), 
                'status-changes' => array('prn','mob'),
                'write-off-charges' => array('prn','mob'),
                'write-off-payments' => array('prn','mob'),
                'view' => array('prn','mob'))
        );
        parent::init($cfg); 
    }
    
    public function viewAction() {
        parent::viewAction();
        $this->view->Plan = new Emms_PmtPlan(new Emms_Loan($this->view->Loans->d->LoanId));
        $this->view->NextDuePayment = $this->view->Plan->getNextDuePayment();
        $savings = new SavingsAccounts();
        $this->view->Savings = $savings->find($this->view->Loans->d->ClientId)->current();
    }
    
    public function statusChangesAction()
    {        
        $id = (int) $this->_request->getParam('id');
        $statusChanges = new LoanStatusChanges();
        try {
            $this->view->loan = $this->_model->getRecord($id)->d;
            $this->view->table = array(
                'head' => $statusChanges->getGridMetadata(),
                'body' => $this->_model->getRecord($id)->r->loanStatusChanges,
                'foot' => array()
            ); 
        } catch (Exception $e) {
            $this->_helper->flashMessenger->addMessage($e->getMessage());
            $this->redirect('/default/auth/deny'); 
        }
        $this->_helper->layout->setLayout('view');
    }
    
    public function writeOffChargesAction()
    {        
        $id = (int) $this->_request->getParam('id');
        $writeOffCharges = new LoanWriteOffCharges();
        try {
            $this->view->loan = $this->_model->getRecord($id)->d;
            $this->view->table = array(
                'head' => $writeOffCharges->getGridMetadata(),
                'body' => $this->_model->getRecord($id)->r->writeOffCharges,
                'foot' => array()
            ); 
            if($this->view->loan->Status == 'Written-Off')
                $this->view->table['rlnk'] = array(
                    'text' => $this->view->translate('Edit'), 
                    'base' => '/loan/write-off-charge/add', 
                    'params' => array('id' => 'WriteOffChargeId'));
        } catch (Exception $e) {
            $this->_helper->flashMessenger->addMessage($e->getMessage());
            $this->redirect('/default/auth/deny'); 
        }
        $this->_helper->layout->setLayout('view');
    }
    
    public function writeOffPaymentsAction()
    {        
        $id = (int) $this->_request->getParam('id');
        $writeOffPayments = new LoanWriteOffPayments();
        try {
            $this->view->loan = $this->_model->getRecord($id)->d;
            $this->view->table = array(
                'head' => $writeOffPayments->getGridMetadata(),
                'body' => $this->_model->getRecord($id)->r->writeOffPayments,
                'foot' => array()
            ); 
            if($this->view->loan->Status == 'Written-Off')
                $this->view->table['rlnk'] = array(
                    'text' => $this->view->translate('Edit'), 
                    'base' => '/loan/write-off-payment/add', 
                    'params' => array('id' => 'WriteOffPaymentId'));
        } catch (Exception $e) {
            $this->_helper->flashMessenger->addMessage($e->getMessage());
            $this->redirect('/default/auth/deny'); 
        }
        $this->_helper->layout->setLayout('view');
    }
    
    public function planAction()
    {
        $id = (int) $this->_request->getParam('id'); 
        try {  
            $this->view->payments = $this->_model->getRecord($id)->r->payments->toArray();
            $this->view->loan = $this->_model->getRecord($id)->d;
            switch($this->view->loan->InterestCalculation) {
                case 'Declining': 
                    $plan = new Emms_PmtPlan(new Emms_Loan($id)); 
                    break;
                case 'Flat': 
                    $plan = new Emms_PmtPlanFlat(new Emms_Loan($id)); 
                    break;
            }
            $schedule = $plan->getSchedule();
            $this->view->table = array(
                'head' => $schedule['meta'],
                'body' => $schedule['data'],
                'foot' => $schedule['total']
            );
        } catch (Exception $e) {
            $this->_helper->flashMessenger->addMessage($e->getMessage());
            $this->redirect('/default/auth/deny'); 
        }
        $this->_helper->layout->setLayout('view');
    }
}
